package com.example.demo.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.commen.R;
import com.example.demo.entity.Attendance;
import com.example.demo.entity.dto.PageQuery;
import com.example.demo.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/attendance")
@CrossOrigin(origins = "*")
public class AttendanceController {

    @Autowired
    private AttendanceService attendanceService;

    // 查询所有考勤信息
    @GetMapping
    public List<Attendance> getAllAttendance() {
        return attendanceService.getAllAttendance();
    }

    // 根据编号查询考勤信息
    @GetMapping("/number/{number}")
    public List<Attendance> getAttendanceByNumber(@PathVariable int number) {
        return attendanceService.getAttendanceByNumber(number);
    }

    // 添加考勤信息
    @PostMapping
    public boolean addAttendance(@RequestBody Attendance attendance) {
        return attendanceService.addAttendance(attendance);
    }

    // 修改考勤信息
    @PutMapping
    public boolean updateAttendance(@RequestBody Attendance attendance) {
        return attendanceService.updateAttendance(attendance);
    }

    // 根据编号删除考勤信息
    @DeleteMapping("/number/{number}")
    public boolean deleteAttendanceByNumber(@PathVariable int number) {
        return attendanceService.deleteAttendanceByNumber(number);
    }

    @PostMapping("/page")
    public R listAttendance(@RequestBody PageQuery pageQuery) {
        IPage<Attendance> result = attendanceService.page(pageQuery);
        return R.ok().data("records", result.getRecords())
                .data("total", result.getTotal());
    }
}